﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;

namespace Projet_WPF.ViewModel
{
    public class FirstViewModel : BaseViewModel
    {
        #region variables
        private DataAccess.User _dataAccessUser = null;
        private bool _closeSignal;
        private string _login;
        private string _erreur;
        #endregion

        #region Commandes
        public ICommand _loginCommand;
        private ICommand _closeCommand;
        #endregion

        #region getter / setter
        public string Login
        {
            get { return _login; }
            set
            {
                if (_login != value)
                {
                    _login = value;
                    OnPropertyChanged("Login");
                }

            }
        }

        public bool CloseSignal
        {
            get { return _closeSignal; }
            set
            {
                if (_closeSignal != value)
                {
                    _closeSignal = value;
                    OnPropertyChanged("CloseSignal");
                }
            }
        }

        public ICommand LoginCommand
        {
            get { return _loginCommand; }
            set { _loginCommand = value; }
        }

        public ICommand CloseCommand
        {
            get { return _closeCommand; }
            set { _closeCommand = value; }
        }

        public string Erreur
        {
            get { return _erreur; }
            set
            {
                if (_erreur != value)
                {
                    _erreur = value;
                    OnPropertyChanged("Erreur");
                }
            }
        }

        #endregion

        /// <summary>
        /// constructeur
        /// </summary>
        public FirstViewModel()
        {
            base.DisplayName = "Page de connexion";
            Login = "";
            Erreur = "Votre login ou mot de passe est erroné";
            
            _dataAccessUser = new Projet_WPF.DataAccess.User();

            _loginCommand = new RelayCommand(param => LoginAccess(param));
            CloseCommand = new RelayCommand(param => Close(), param => true);
        }

        /// <summary>
        /// réponse à la commande click
        /// </summary>
        private void LoginAccess(object parameter)
        {
            var password = parameter as PasswordBox;

            if (_dataAccessUser.Connect(Login, password.Password))
            {
                bool visible = true;

                if (_dataAccessUser.GetRole(Login).ToLower().Equals("infirmière"))
                    visible = false;

                View.ControlView window = new View.ControlView();
                ViewModel.ControlViewModel vm = new ControlViewModel(_dataAccessUser.GetUser(Login), visible);

                window.DataContext = vm;
                window.Show();
                CloseSignal = true;
            }
        }

        private void Close()
        {
            CloseSignal = true;
        }
    }
}
